<body> <divid="title"> <h1>Load and save JSON documents</h1> <p> Load a JSON document: <inputtype="file"id="loadDocument"value="Load" /> </p> <p> Save a JSON document: <inputtype="button"id="saveDocument"value="Save" /> </p> <h1>set and get JSON</h1> <p> <buttonid="setJSON">Set JSON</button> <buttonid="getJSON">Get JSON</button> </p> </div>
<ul> <li>the field <code>example_id</code> and its value are read-only</li> <li>the field <code>example_name</code> is read-only but has an editable value</li> <li>the field <code>example_age</code> and its value are editable</li> </ul>
<h1>Item templates</h1> <p> Using item templates, the options in the context menu under "insert" and "append" can be extended with extra options, containing a domain specific template like a "Person", "Contact", "Order", "Address", etc. </p>
<scripttype="text/javascript"> var container1 = document.getElementById('container1'); var options1 = { mode: 'text', error: function (err) { alert(err.toString()); }, change: function () { if (editor1 != null) { editor2.setText(editor1.getText()); editor2.expandAll(); } } }; var json = { example_id: 123456, example_name: 'John', example_age: 32 }; var editor1 = new JSONEditor(container1, options1, json); var container2 = document.getElementById('container2'); var options2 = { mode: "tree", onError: function (err) { alert(err.toString()); }, onChange: function () { if (editor2 != null) { editor1.setText(editor2.getText()); editor1.expandAll(); } }, onEditable: function (node) { // node is an object like: // { // field: 'FIELD', // value: 'VALUE', // path: ['PATH', 'TO', 'NODE'] // } switch (node.field) { case'example_id': returnfalse; case'example_name': return { field: false, value: true }; default: returntrue; } }, templates: [{ text: 'Person', title: 'Insert a Person Node', className: 'jsoneditor-type-object', field: 'PersonTemplate', value: { 'firstName': 'John', 'lastName': 'Do', 'age': 28 } }, { text: 'Address', title: 'Insert a Address Node', field: 'AddressTemplate', value: { 'street': '', 'city': '', 'state': '', 'ZIP code': '' } } ], autocomplete: { applyTo: ['value'], getOptions: function (text, path, input, editor) { returnnewPromise(function (resolve, reject) { var options = extractUniqueWords(editor.get()); if (options.length > 0) resolve(options); else reject(); }); } } }; var editor2 = new JSONEditor(container2, options2, json); // set json document.getElementById('setJSON').onclick = function () { var json = { "definitions": { "Beef": { "type": "object", "properties": { "BID": { "type": "string", "id": true }, "Species": { "type": "string" }, "Birthday": { "type": "string" }, "Color": { "type": "string" }, "Status": { "type": "int" } } } } }; editor1.set(json); editor2.set(json); }; // get json document.getElementById('getJSON').onclick = function () { var json = editor1.get(); alert(JSON.stringify(json, null, 2)); }; // Load a JSON document FileReaderJS.setupInput(document.getElementById('loadDocument'), { readAsDefault: 'Text', on: { load: function (event, file) { editor1.setText(event.target.result); editor2.setText(event.target.result); } } }); // Save a JSON document document.getElementById('saveDocument').onclick = function () { // Save Dialog fname = window.prompt("Save as..."); // Check json extension in file name if (fname.indexOf(".") == -1) { fname = fname + ".json"; } else { if (fname.split('.').pop().toLowerCase() == "json") { // Nothing to do } else { fname = fname.split('.')[0] + ".json"; } } var blob = new Blob([editor1.getText()], { type: 'application/json;charset=utf-8' }); saveAs(blob, fname); }; // helper function to extract all unique words in the keys and values of a JSON object functionextractUniqueWords(json) { return _.uniq(_.flatMapDeep(json, function (value, key) { return _.isObject(value) ? [key] : [key, String(value)] })) } </script> </body>
Ace is an embeddable code editor written in JavaScript. It matches the features and performance of native editors such as Sublime, Vim and TextMate. It can be easily embedded in any web page and JavaScript application. Ace is maintained as the primary editor for Cloud9 IDE and is the successor of the Mozilla Skywriter (Bespin) project.